package phoenix.web.page_objects.features;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.TestException;
import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.PageBaseWebExt;
import phoenix.core.config.extensions.ConfigExt;
import phoenix.web.locators.base.features.InboxPageLocatorBase;
import phoenix.web.locators.base.features.MessengerLocatorsBase;

import java.util.List;

/**
 * Created by AlexGolovko on 06.11.2014.
 * FGF communication related page
 */
public class InboxPage extends PageBaseWebExt
{
	/**
	 * Initializes PageBaseWebExt
	 * @param p_manager PhoenixManager instance
	 */
	public InboxPage(PhoenixManager p_manager)
	{
		super(p_manager);
	}

	public InboxPageLocatorBase elements()
	{
		return new phoenix.web.locators.foreigngirlfriend.features.InboxPage();
	}

	public void typeSubject(String p_value)
	{
		wd().waitForPresence(elements().inputSubject, "Subject input");
		wd().click(elements().inputSubject);
		wd().type(elements().inputSubject, p_value);
	}

	public void typeMessage(String p_value)
	{
		wd().waitForPresence(elements().inputMessage, "Message input");
		wd().typeByIdJs(elements().inputMessage, p_value);
	}

	public void clickStartChat()
	{
		wd().waitForPresence(elements().btnStartChat, "Start chat button");
		wd().click(elements().btnStartChat);
	}

	public void clickNewLetter()
	{
		wd().waitForPresence(elements().btnNewLetter, "New Letter button");
		wd().click(elements().btnNewLetter);
	}

	public boolean isNewLetterBtnDisplayed()
	{
		return wd().isElementDisplayed(elements().btnNewLetter);
	}

	public void clickSendLetter()
	{
		wd().waitForPresence(elements().btnSendLetter, "Button Send Letter");
		wd().click(elements().btnSendLetter);
	}

	public void clickSend()
	{
		wd().waitForPresence(elements().btnSend, "Button Send Message");
		wd().click(elements().btnSend);
	}

	public boolean isSubjectPresent()
	{
		wd().waitForPresence(elements().blockSubject, "Subject text block");
		return wd().isElementPresent(elements().blockSubject);
	}

	public String getSubject()
	{
		return wd().getText(elements().blockSubject);
	}

	public boolean isLetterPresent()
	{
		wd().waitForPresence(elements().blockMessageText, "Letter text block");
		return wd().isElementPresent(elements().blockMessageText);
	}

	public String getLetter()
	{
		return wd().getText(elements().blockMessageText);
	}

	public boolean isLetterPresent(String p_messageText, String p_subjectText)
	{
		Boolean isMessageFound = false;
		Boolean isSubjectFound = false;

		wd().waitForVisibility(elements().blockLetterList, "Block 'Letter List'");
		WebElement elMessages = wd().getWebElement(elements().blockLetterList);

		List<WebElement> l_subject = elMessages.findElements(elements().elementSubject);
		List<WebElement> l_message = elMessages.findElements(elements().elementText);

		log().info("Found subjects: " + l_subject.size());
		log().info("Found messages: " + l_message.size());

		for (int i = 0; i < l_subject.size(); i++)
		{
			System.out.println(l_subject.get(i).getText());

			if (l_subject.get(i).getText().contains(p_subjectText))
			{
				log().info("Subject '" + p_subjectText + "' has been found successfully.");
				isSubjectFound = true;
			}
		}

		if (!isSubjectFound)
		{
			log().warn("Unable to find subject " + p_subjectText);
			return false;
		}

		for (int i = 0; i < l_message.size(); i++)
		{
			System.out.println(l_message.get(i).getText());

			if (l_message.get(i).getText().substring(0, 30).contains(p_messageText.substring(0, 30)))
			{
				log().info("Message '" + p_messageText + "' has been found successfully.");
				isMessageFound = true;
			}
		}

		if (!isMessageFound)
		{
			log().warn("Unable to find message " + p_messageText);
			return false;
		}

		return true;
	}
}
